package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.myview.SwitchView;

import static com.wq.cycling.R.id.switchview1;

/**
 * 设防
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class Fortify extends BackHandledFragment {

	private View rootView;
	private SwitchView switchView1;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_fortify, null);
		initView();
		initData();
		return rootView;
	}

	private void initData(){
		ApiUtil.getApiService().devicegetsetting(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						switchView1.setOpened(entity.getData().isDefence());
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}


	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.title_fortify);
		switchView1 = ((SwitchView) rootView.findViewById(switchview1));
		switchView1.setChangeListener(listener);

	}

	public static Fortify newInstance() {
		Fortify fragment = new Fortify();
		return fragment;
	}

	public SwitchView.ChangeListener listener = new SwitchView.ChangeListener() {
		@Override
		public void listener(View v, boolean ischecked) {
			switch (v.getId()){
				case switchview1:
					submit(1,ischecked);
					break;
				default:
					break;
			}
		}
	};
	public void submit(final int index,final boolean ischecked){
		ApiUtil.getApiService().setDefence(DemoApplication.getToken(),DemoApplication.getDeviceid(),switchView1.isOpened()+"").enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}else{
						switchView1.setOpened(!switchView1.isOpened());
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				switchView1.setOpened(!switchView1.isOpened());
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}
	
}
